﻿using AnlianCommon.Common.Domains.ProductionManagement;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace SheepCommon.API.SheepCommands.PregnancyTestes
{
    public class PregnancyTestUpdateHandler : IRequestHandler<PregnancyTestUpdateCommand, Result>
    {
        private readonly IBaseRepository<PregnancyTest> _pregnancyTestRepository;

        public PregnancyTestUpdateHandler(IBaseRepository<PregnancyTest> pregnancyTestRepository)
        {
            _pregnancyTestRepository = pregnancyTestRepository;
        }
        /// <summary>
        /// 种母妊检修改业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(PregnancyTestUpdateCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Deleted };
            try
            {
                var pregnancyTest = await(await _pregnancyTestRepository.GetAll()).Where(c => c.PregnancyTestId == request.PregnancyTestId).FirstOrDefaultAsync();
                if (pregnancyTest != null)
                {
                    pregnancyTest.UpdatedOwnerId = "张三";
                    pregnancyTest.UpdatedTime = DateTime.UtcNow;
                    var res = await _pregnancyTestRepository.Update(pregnancyTest);
                    if (res > 0)
                    {
                        result.Code = (int)CommonErrorCode.Success;
                        result.Msg = "修改成功";
                    }
                    else
                    {
                        result.Code = (int)CommonErrorCode.Deleted;
                        result.Msg = "修改失败";
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
